package com.example.sportplay.controller;

import com.alibaba.fastjson.JSON;

import com.example.sportplay.bean.QueryInfo;
import com.example.sportplay.bean.User;
import com.example.sportplay.bean.car;
import com.example.sportplay.dao.CarDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class CarController {

    @Autowired
    private CarDao carDao;
    @CrossOrigin
    @RequestMapping("/carlist")
    public String getCarList(){






        List<User> cars = carDao.getAllCar();
        HashMap<String, Object> res = new HashMap<>();

        res.put("data",cars);

        String users_json = JSON.toJSONString(res);
        System.out.println(cars);
        return users_json;
    }
    @RequestMapping("/ydcw")
    public String ydcw(String carnumber,String name){

        List list = carDao.ydcwfindbycarnumber(carnumber);
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",list);

        String users_json = JSON.toJSONString(res);
        System.out.println("名字是这个");
     System.out.println(list);//username是一个list
        car mm=new car();
        car mmm=new car();

       
            mm=(car) list.get(0);
           String username=mm.getUsername();
//        String name=mm.getName();
//        System.out.println(name);
//        List list2 = carDao.ydcwfindbycarnumberfindname(carnumber);//找name
//        mmm=(car) list2.get(0);
//
//        String name=mmm.getUsername();
        System.out.println(username);
        System.out.println(carnumber);
        System.out.println(name);
        int i = carDao.deleteCar(name);
        int ydcwxie = carDao.ydcwxie(username,carnumber,name);//插入
        System.out.println(ydcwxie);


        return users_json;
    }
    @CrossOrigin
    @RequestMapping("/carlist2")
    public String getCarList2(){


        List<User> alljiaofei = carDao.getAlljiaofei();

        HashMap<String, Object> res = new HashMap<>();

        res.put("data",alljiaofei);

        String users_json = JSON.toJSONString(res);
        System.out.println(alljiaofei);
        return users_json;
    }
    @RequestMapping("/txtfindcar")
    public String getFindCarList(QueryInfo queryInfo){
        //  System.out.println(queryInfo);
//        int numbers = userDao.getUserCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
//        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<User> users = carDao.getFindAllCar("%"+queryInfo.getQuery()+"%");
        HashMap<String, Object> res = new HashMap<>();
        // res.put("numbers",numbers);
        res.put("data",users);

        String users_json = JSON.toJSONString(res);
        // System.out.println("总条数："+users_json);
        return users_json;
    }
    @RequestMapping("/findjiaofei")
    public String getFindCarList2(QueryInfo queryInfo){
        //  System.out.println(queryInfo);
//        int numbers = userDao.getUserCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
//        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<User> users = carDao.getAllfindjiaofeiss(queryInfo.getQuery());
        HashMap<String, Object> res = new HashMap<>();
        // res.put("numbers",numbers);
        res.put("data",users);
        System.out.println("总条数121213121312：");
        System.out.println(users);
        System.out.println(queryInfo.getQuery());

        String users_json = JSON.toJSONString(res);
        // System.out.println("总条数："+users_json);
        return users_json;
    }
    @CrossOrigin
    @RequestMapping("/addCar")
    public String addUser(@RequestBody car ca){
        System.out.println(ca);

        int i = carDao.addCar(ca);
        String str = i >0?"success":"error";
        return str;
    }
    @CrossOrigin


    @RequestMapping("/findcar")
    public String findcar(){

        List<User> allCar = carDao.getAllCar();
        HashMap<String, Object> res = new HashMap<>();
        // res.put("numbers",numbers);
        res.put("data",allCar);

        String users_json = JSON.toJSONString(res);
        System.out.println("总条数："+users_json);
        return users_json;
    }
    @RequestMapping("/pdif")
    public String pdif(String carnumber){

        List<User> allCar = carDao.pdif(carnumber);
        HashMap<String, Object> res = new HashMap<>();
        // res.put("numbers",numbers);
        res.put("data",allCar);
        System.out.println(allCar);
        System.out.println("allCar");
        System.out.println(allCar.size());

        int pdjg = 0;


        if(allCar.size()!=0)

            pdjg=1;
        String str = pdjg ==0?"success":"error";
        return str;




    }
    @RequestMapping("/findcarplace")
    public String findcarplace(){

        List<User> allCar = carDao.getAllCarplace();
        HashMap<String, Object> res = new HashMap<>();
        // res.put("numbers",numbers);
        res.put("data",allCar);

        String users_json = JSON.toJSONString(res);
        System.out.println("总条数："+users_json);
        return users_json;
    }
    @RequestMapping("/editCar")
    public String editUser(@RequestBody car car){

        int i = carDao.editCar(car);
        String str = i >0?"success":"error";
        return str;
    }
    @RequestMapping("/getnumber")
    public String getnumber(){

        List<User> findAllCarkong = carDao.getFindAllCarkong();
        HashMap<String, Object> res = new HashMap<>();
        // res.put("numbers",numbers);
        res.put("data",findAllCarkong);

        String users_json = JSON.toJSONString(res);
        // System.out.println("总条数："+users_json);
        return users_json;
    }



    @RequestMapping("/txtfindcarkong")
    public String getFindCarListkong(){
        //  System.out.println(queryInfo);
//        int numbers = userDao.getUserCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
//        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<User> users = carDao.getFindAllCarkong();
        HashMap<String, Object> res = new HashMap<>();
        // res.put("numbers",numbers);
        res.put("data",users);

        String users_json = JSON.toJSONString(res);
        // System.out.println("总条数："+users_json);
        return users_json;
    }
    @RequestMapping("/txtfindbus")
    public String getAllBus(QueryInfo queryInfo){
        //  System.out.println(queryInfo);
//        int numbers = userDao.getUserCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
//        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<User> users = carDao.getAllBus();
        HashMap<String, Object> res = new HashMap<>();
        // res.put("numbers",numbers);
        res.put("data",users);

        String users_json = JSON.toJSONString(res);
        System.out.println("总条数："+users_json);
        return users_json;
    }
    @CrossOrigin
    @RequestMapping("/deleteCar")
    public String deleteCar(String name){
        System.out.println(name);
        int i = carDao.deleteCar(name);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/editUserqianshiru")
    public String editUserqianshiru(@RequestBody car car){
        System.out.println(car);
        System.out.println("模拟用户");

        int i = carDao.editUserqianshiru(car);
//        int iii = userDao.editUserqianqianqian(user);
//        int i = userDao.editUserqian(user);
        String str = i >0?"success":"error";
        System.out.println(str);
        return str;
    }
    @RequestMapping("/goout")
    public String goout(String carnumber){
        System.out.println(carnumber);
        System.out.println("模拟用户驶出");
        List pdif = carDao.pdif(carnumber);
        System.out.println(pdif);
        car mmmm=new car();



        mmmm=(car) pdif.get(0);
        String name=mmmm.getName();
        System.out.println(name);

        int i = carDao.goout(name);
//        int iii = userDao.editUserqianqianqian(user);
//        int i = userDao.editUserqian(user);
        String str = i >0?"success":"error";
        System.out.println(str);
        return str;
    }
}
